<?php
/**
 * Created by PhpStorm.
 * User: VJLau
 * Date: 2018/2/5
 * Time: 下午12:29
 */

//所有ajax操作都放在这里
$action = $_GPC['action'];
$msg = '';
switch ($action) {
    //目的地列表
    case "destination_list":
        $region_id = $_GPC['r_id'];
        if (empty($region_id) || intval($region_id) <= 0) ajax_error('参数错误');
        $dests = pdo_fetchall("SELECT * FROM " . tablename('bontrip_dest') .
            " WHERE region_id = " . $region_id . " AND uniacid= '{$uniacid}' AND deleted_at is null ORDER BY sort ASC,id DESC");
        //处理目的地数据，每三个为一组
        $num = ceil(count($dests) / 3);
        $dests_list = array();
        for ($i = 0; $i < $num; $i++) {
            for ($j = 0; $j < 3; $j++) {
                if (!empty($dests[$j + $i * 3])) {
                    $dests[$j + $i * 3]['thumb'] = tomedia($dests[$j + $i * 3]['thumb'], true);
                    $dests_list[$i][] = $dests[$j + $i * 3];
                } else {
                    $dests_list[$i][] = null;
                }
            }
        }
        ajax_success($dests_list);
        break;
    case "single_list":
        //单品列表
        $pindex = max(1, intval($_GPC['page']));
        $psize = 4;
        $id = intval($_GPC['dest_id']);
        $single = pdo_fetchall("SELECT sg.*,currency.exchange_rate FROM " . tablename('bontrip_single')
            . " AS sg LEFT JOIN " . tablename('bontrip_currency') . " AS currency ON currency.id = sg.currency_id WHERE sg.dest_id = '{$id}' and sg.deleted_at is null"
            . " ORDER BY sg.sort ASC,sg.id DESC LIMIT "
            . ($pindex - 1) * $psize . ',' . $psize);
        $total = pdo_fetchcolumn("SELECT COUNT(*) FROM " . tablename('bontrip_single')
            . " AS sg LEFT JOIN " . tablename('bontrip_currency') . " AS currency ON currency.id = sg.currency_id WHERE sg.dest_id = '{$id}' and sg.deleted_at is null");

        $max_page = ceil($total / $psize);
        $is_next = true;

        if ($max_page <= $pindex) {
            $is_next = false;
        }
        //处理单品数据，每两个为一组
        $num = ceil(count($single) / 2);
        $single_list = array();
        for ($i = 0; $i < $num; $i++) {
            for ($j = 0; $j < 2; $j++) {
                if (!empty($single[$j + $i * 2])) {
                    $single[$j + $i * 2]['thumb'] = tomedia($single[$j + $i * 2]['thumb'], true);
                    $single[$j + $i * 2]['adult_price'] = round($single[$j + $i * 2]['adult_price'] * $single[$j + $i * 2]['exchange_rate']);
                    $single_list[$i][] = $single[$j + $i * 2];
                } else {
                    $single_list[$i][] = null;
                }
            }
        }
        $data = array(
            'list' => $single_list,
            'is_next' => $is_next,
            'page' => $pindex
        );
        ajax_success($data);
        break;
    case "oneday_list":
        //一日游
        $pindex = max(1, intval($_GPC['page']));
        $psize = 2;
        $id = intval($_GPC['dest_id']);
        $onedays = pdo_fetchall("SELECT od.*,currency.exchange_rate FROM " . tablename('bontrip_oneday')
            . " AS od LEFT JOIN " . tablename('bontrip_currency') . " AS currency ON currency.id = od.currency_id WHERE od.dest_id = '{$id}' and od.deleted_at is null"
            . " ORDER BY od.sort ASC,od.id DESC LIMIT "
            . ($pindex - 1) * $psize . ',' . $psize);
        $total = pdo_fetchcolumn("SELECT COUNT(*) FROM " . tablename('bontrip_oneday')
            . " AS od LEFT JOIN " . tablename('bontrip_currency') . " AS currency ON currency.id = od.currency_id WHERE od.dest_id = '{$id}' and od.deleted_at is null");

        $max_page = ceil($total / $psize);
        $is_next = true;

        if ($max_page <= $pindex) {
            $is_next = false;
        }
        if (!empty($onedays)) {
            foreach ($onedays as $key => $oneday) {
                $onedays[$key]['adult_price'] = round($oneday['adult_price'] * $oneday['exchange_rate']);
                $onedays[$key]['thumb'] = tomedia($onedays[$key]['thumb'], true);
                $onedays[$key]['tag'] = empty($oneday['tag']) ? null : explode(',', $oneday['tag']);
            }
        }
        $data = array(
            'list' => $onedays,
            'is_next' => $is_next,
            'page' => $pindex
        );
        ajax_success($data);
        break;
    case "quality_list":
        $pindex = max(1, intval($_GPC['page']));
        $id = intval($_GPC['dest_id']);
        $psize = 2;
        $qualitys = pdo_fetchall("SELECT ql.*,currency.exchange_rate FROM " . tablename('bontrip_quality')
            . " AS ql LEFT JOIN " . tablename('bontrip_currency') . " AS currency ON currency.id = ql.currency_id"
            . " RIGHT JOIN " . tablename('bontrip_quality_dest') . "AS qd ON ql.id = qd.quality_id"
            . " WHERE qd.dest_id = '{$id}' and ql.deleted_at is null"
            . " ORDER BY ql.sort ASC,ql.id DESC LIMIT "
            . ($pindex - 1) * $psize . ',' . $psize);
        $total = pdo_fetchcolumn("SELECT COUNT(*)FROM " . tablename('bontrip_quality')
            . " AS ql LEFT JOIN " . tablename('bontrip_currency') . " AS currency ON currency.id = ql.currency_id"
            . " RIGHT JOIN " . tablename('bontrip_quality_dest') . "AS qd ON ql.id = qd.quality_id"
            . " WHERE qd.dest_id = '{$id}' and ql.deleted_at is null");
        $max_page = ceil($total / $psize);
        $is_next = true;

        if ($max_page <= $pindex) {
            $is_next = false;
        }
        if (!empty($qualitys)) {
            foreach ($qualitys as $key => $quality) {
                $qualitys[$key]['adult_price'] = round($quality['adult_price'] * $quality['exchange_rate']);
                $qualitys[$key]['thumb'] = tomedia($qualitys[$key]['thumb'], true);
                $qualitys[$key]['tag'] = empty($quality['tag']) ? null : explode(',', $quality['tag']);
            }
        }
        $data = array(
            'list' => $qualitys,
            'is_next' => $is_next,
            'page' => $pindex
        );
        ajax_success($data);
        break;
    case "line_list":
        //线路
        $pindex = max(1, intval($_GPC['page']));
        $psize = 2;
        $lines = pdo_fetchall("SELECT * FROM " . tablename('bontrip_line')
            . " WHERE deleted_at is null"
            . " ORDER BY sort ASC,id DESC LIMIT "
            . ($pindex - 1) * $psize . ',' . $psize);
        $total = pdo_fetchcolumn("SELECT COUNT(*) FROM " . tablename('bontrip_line')
            . " WHERE deleted_at is null");

        $max_page = ceil($total / $psize);
        $is_next = true;

        if ($max_page <= $pindex) {
            $is_next = false;
        }
        if (!empty($lines)) {
            foreach ($lines as $key => $line) {
                $lines[$key]['thumb'] = tomedia($lines[$key]['thumb'], true);
                $lines[$key]['tag'] = empty($line['tag']) ? null : explode(',', $line['tag']);
            }
        }
        $data = array(
            'list' => $lines,
            'is_next' => $is_next,
            'page' => $pindex
        );
        ajax_success($data);
        break;
    case "order_list":
        $pindex = max(1, intval($_GPC['page']));
        $type = $_GPC['type'];
        $psize = 4;
        $uniacid = $_W['uniacid'];
        $member = unserialize(base64_decode($_SESSION['vj_member']));
        switch ($type) {
            case "single":
                //单品
                $orders = pdo_fetchall("SELECT * FROM " . tablename('bontrip_single_order')
                    . "  WHERE uniacid = '{$uniacid}' AND member_id = {$member['id']} AND deleted_at is null"
                    . " ORDER BY id DESC LIMIT "
                    . ($pindex - 1) * $psize . ',' . $psize);
                $total = pdo_fetchcolumn("SELECT COUNT(*) FROM " . tablename('bontrip_single_order')
                    . "  WHERE uniacid = '{$uniacid}' AND member_id = {$member['id']} AND deleted_at is null");

                $max_page = ceil($total / $psize);
                $is_next = true;

                if ($max_page <= $pindex) {
                    $is_next = false;
                }
                break;
            case "oneday":
                //一日游
                $orders = pdo_fetchall("SELECT * FROM " . tablename('bontrip_oneday_order')
                    . "  WHERE uniacid = '{$uniacid}' AND member_id = {$member['id']} AND deleted_at is null"
                    . " ORDER BY id DESC LIMIT "
                    . ($pindex - 1) * $psize . ',' . $psize);
                $total = pdo_fetchcolumn("SELECT COUNT(*) FROM " . tablename('bontrip_oneday_order')
                    . "  WHERE uniacid = '{$uniacid}' AND member_id = {$member['id']} AND deleted_at is null");


                $max_page = ceil($total / $psize);
                $is_next = true;

                if ($max_page <= $pindex) {
                    $is_next = false;
                }
                break;
            case "quality":
                //精品游
                $orders = pdo_fetchall("SELECT * FROM " . tablename('bontrip_oneday_order')
                    . "  WHERE uniacid = '{$uniacid}' AND member_id = {$member['id']} AND deleted_at is null"
                    . " ORDER BY id DESC LIMIT "
                    . ($pindex - 1) * $psize . ',' . $psize);
                $total = pdo_fetchcolumn("SELECT COUNT(*) FROM " . tablename('bontrip_oneday_order')
                    . "  WHERE uniacid = '{$uniacid}' AND member_id = {$member['id']} AND deleted_at is null");

                $max_page = ceil($total / $psize);
                $is_next = true;

                if ($max_page <= $pindex) {
                    $is_next = false;
                }
                break;
            case "insurance":
                //保险
                $orders = pdo_fetchall("SELECT * FROM " . tablename('bontrip_insurance_order')
                    . "  WHERE uniacid = '{$uniacid}' AND member_id = {$member['id']} AND deleted_at is null"
                    . " ORDER BY id DESC LIMIT "
                    . ($pindex - 1) * $psize . ',' . $psize);
                $total = pdo_fetchcolumn("SELECT COUNT(*) FROM " . tablename('bontrip_insurance_order')
                    . "  WHERE uniacid = '{$uniacid}' AND member_id = {$member['id']} AND deleted_at is null");

                $max_page = ceil($total / $psize);
                $is_next = true;

                if ($max_page <= $pindex) {
                    $is_next = false;
                }
                break;
            default:
                ajax_error('参数错误');
                break;
        }
        if (!empty($orders)) {
            foreach ($orders as $kay => $order) {
                $orders[$kay]['created_at'] = date('Y-m-d', $order['created_at']);
                $orders[$kay]['status_str'] = ($order['status'] == '1') ? '已支付' : '未支付';
                $orders[$kay]['thumb'] = tomedia($order['thumb'], true);
                if ($type != 'insurance') {
                    $orders[$kay]['departure_date'] = date('Y-m-d', $order['departure_date']);
                } else {
                    $orders[$kay]['start_day'] = date('Y-m-d', $order['start_day']);
                    $orders[$kay]['finish_day'] = date('Y-m-d', $order['finish_day']);
                }
            }
        }
        $data = array(
            'list' => $orders,
            'is_next' => $is_next,
            'page' => $pindex
        );
        ajax_success($data);
        break;
    case "add_insurance_order":
        //生成保险订单
        $insurance_id = intval($_GPC['insurance_id']);
        $start_day = trim($_GPC['start_day']);
        $finish_day = trim($_GPC['finish_day']);
        $adult_number = intval($_GPC['adult_number']);
        $child_number = intval($_GPC['child_number']);
        $contact_name = trim($_GPC['contact_name']);
        $contact_mobile = trim($_GPC['contact_mobile']);
        $contact_email = trim($_GPC['contact_email']);
        $insured_info = $_SESSION['insured'];
        $member = unserialize(base64_decode($_SESSION['vj_member']));

        //验证
        if (empty($insurance_id)) ajax_error('参数错误');
        if (empty($start_day)) ajax_error('请选择出发时间');
        if (empty($finish_day)) ajax_error('请选择返回时间');
        if ($adult_number == 0 && $child_number == 0) ajax_error('请选择人数');
        if (empty($contact_name)) ajax_error('请填写投保人');
        if (empty($contact_mobile)) ajax_error('请填写投保人手机');
        if (empty($contact_email)) ajax_error('请填写投保人邮箱');
        if (empty($insured_info)) ajax_error('请填写被保人');
        if ((count($insured_info['adult']) != $adult_number) || count($insured_info['child']) != $child_number) ajax_error('选择人数与信息不匹配');

        $insurance = pdo_fetch("SELECT i.*,ip.id as ip_id,ip.adult_price,ip.child_price FROM" . tablename('bontrip_insurance')
            . " AS i LEFT JOIN " . tablename('bontrip_insurance_price') . " AS ip ON ip.insurance_id = i.id"
            . " WHERE ip.id = {$insurance_id}");
        if (empty($insurance)) ajax_error('保险信息不存在');

        //获取被保人信息
        $adult_ids = array();
        $adult_insureds = array();
        if (!empty($insured_info['adult'])) {
            foreach ($insured_info['adult'] as $adult) {
                array_push($adult_ids, $adult['id']);
            }
            $adult_insureds = pdo_fetchall("SELECT * FROM" . tablename('bontrip_insurance_member')
                . " WHERE member_id = {$member['id']} AND uniacid = {$uniacid} AND id in(" . implode(',', $adult_ids) . ") AND deleted_at is null");
        }

        $child_ids = array();
        $child_insureds = array();
        if (!empty($insured_info['child'])) {
            foreach ($insured_info['child'] as $child) {
                array_push($child_ids, $child['id']);
            }
            $child_insureds = pdo_fetchall("SELECT * FROM" . tablename('bontrip_insurance_member')
                . " WHERE member_id = {$member['id']} AND uniacid = {$uniacid} AND id in(" . implode(',', $child_ids) . ") AND deleted_at is null");
        }

        $data = array(
            'uniacid' => $uniacid,
            'member_id' => $member['id'],
            'insurance_id' => $insurance['id'],
            'name' => $insurance['name'],
            'security_desc' => $insurance['security_desc'],
            'notice_desc' => $insurance['notice_desc'],
            'start_day' => $start_day,
            'finish_day' => $finish_day,
            'thumb' => $insurance['thumb'],
            'adult_price' => $insurance['adult_price'],
            'child_price' => $insurance['child_price'],
            'adult_number' => $adult_number,
            'child_number' => $child_number,
            'adult_members' => serialize($adult_insureds),
            'child_members' => serialize($child_insureds),
            'total_price' => ($adult_number * $insurance['adult_price']) + ($child_number * $insurance['child_price']),
            'status' => 0,
            'contact_name' => $contact_name,
            'contact_mobile' => $contact_mobile,
            'contact_email' => $contact_email,
            'order_no' => orderNo($member['id']),
            'created_at' => time(),
            'updated_at' => time()
        );
        pdo_insert('bontrip_insurance_order', $data);
        if ($id = pdo_insertid()) {
            ajax_success($id);
        } else {
            $msg = '添加失败';
            ajax_error($msg);
        }
        break;
    case "add_payment_order":
        $id = intval($_GPC['goods_id']);
        $type = trim($_GPC['type']);
        $departure_date = trim($_GPC['departure_date']);
        $adult_number = intval($_GPC['adult_number']);
        $child_number = intval($_GPC['child_number']);
        $baby_number = intval($_GPC['baby_number']);
        $adult_members = $_GPC['adult_members'];
        $child_members = $_GPC['child_members'];
        $baby_members = $_GPC['baby_members'];
        $contact_name = trim($_GPC['contact_name']);
        $contact_mobile = trim($_GPC['contact_mobile']);
        $contact_email = trim($_GPC['contact_email']);
        $member = unserialize(base64_decode($_SESSION['vj_member']));
        $uniacid = $_W['uniacid'];
        $sku_id = intval($_GPC['sku_id']);


        //验证
        if (empty($id)) ajax_error('参数错误');
        if (empty($type)) ajax_error('参数错误');
        if (empty($departure_date)) ajax_error('请选择出行时间');
        if ($adult_number == 0 && $child_number == 0 && $baby_number == 0) ajax_error('请选择人数');
        if (empty($contact_name)) ajax_error('请填写联系人');
        if (empty($contact_mobile)) ajax_error('请填写联系人手机');
        if (empty($contact_email)) ajax_error('请填写联系人邮箱');
        if ($adult_number != 0 && (empty($adult_members) || count($adult_members) != $adult_number)) ajax_error('成人旅客信息错误');
        if ($child_number != 0 && (empty($child_members) || count($child_members) != $child_number)) ajax_error('儿童旅客信息错误');
        if ($adult_number != 0 && (empty($adult_members) || count($adult_members) != $adult_number)) ajax_error('婴儿旅客信息错误');

        switch ($type) {
            case "single":
                $goods = pdo_fetch("SELECT sg.*,currency.exchange_rate FROM " . tablename('bontrip_single')
                    . " AS sg LEFT JOIN " . tablename('bontrip_currency') . " AS currency ON currency.id = sg.currency_id WHERE sg.id = '{$id}' and sg.uniacid = '{$uniacid}' and sg.deleted_at is null");
                break;
            case "oneday":
                $goods = pdo_fetch("SELECT od.*,currency.exchange_rate FROM " . tablename('bontrip_oneday')
                    . " AS od LEFT JOIN " . tablename('bontrip_currency') . " AS currency ON currency.id = od.currency_id WHERE od.id = '{$id}' and od.uniacid = '{$uniacid}' and od.deleted_at is null");
                break;
            case "quality":
                $goods = pdo_fetch("SELECT ql.*,currency.exchange_rate FROM " . tablename('bontrip_quality')
                    . " AS ql LEFT JOIN " . tablename('bontrip_currency') . " AS currency ON currency.id = ql.currency_id WHERE ql.id = '{$id}' and ql.uniacid = '{$uniacid}' and ql.deleted_at is null");
                break;
            default:
                ajax_error('参数错误');
                break;
        }

        if (empty($goods)) {
            ajax_error('商品不存在或已下架');
        }
        if (!empty($sku_id)) {
            $sku = pdo_fetch("select * from " . tablename('bontrip_sku')
                . " where id={$sku_id} and deleted_at is null");

            if (empty($sku)) ajax_error('sku参数不存在');
        }
        $order_no = orderNo($member['id']);
        //计算金额
        $total_price = (round($goods['adult_price'] * $goods['exchange_rate']) * $adult_number) + (round($goods['child_price'] * $goods['exchange_rate']) * $child_number) + (round($goods['baby_price'] * $goods['exchange_rate']) * $baby_number);
        //生成订单
        $data = array(
            'uniacid' => $uniacid,
            'member_id' => $member['id'],
            'tag' => $goods['tag'],
            'name' => $goods['name'],
            'remark' => $goods['remark'],
            'thumb' => $goods['thumb'],
            'sku' => serialize($sku),
            'departure_date' => strtotime($departure_date),
            'currency_id' => $goods['currency_id'],
            'exchange_rate' => $goods['exchange_rate'],
            'adult_price' => $goods['adult_price'],
            'child_price' => $goods['child_price'],
            'baby_price' => $goods['baby_price'],
            'adult_number' => $adult_number,
            'child_number' => $child_number,
            'baby_number' => $baby_number,
            'adult_members' => serialize($adult_members),
            'child_members' => serialize($child_members),
            'baby_members' => serialize($baby_members),
            'total_price' => $total_price,
            'status' => 0,
            'contact_name' => $contact_name,
            'contact_mobile' => $contact_mobile,
            'contact_email' => $contact_email,
            'order_no' => $order_no,
            'created_at' => time(),
            'updated_at' => time()
        );

        if ($type == 'single') {
            $data['single_id'] = $id;
            pdo_insert('bontrip_single_order', $data);

        } else if ($type == 'oneday') {
            $data['oneday_id'] = $id;
            $data['label'] = $goods['label'];
            $data['introduction'] = $goods['introduction'];
            pdo_insert('bontrip_oneday_order', $data);

        } else if ($type == 'quality') {
            $data['quality_id'] = $id;
            $data['label'] = $goods['label'];
            $data['introduction'] = $goods['introduction'];
            pdo_insert('bontrip_quality_order', $data);

        }

        if (pdo_insertid()) {
            ajax_success($data);
        } else {
            $msg = '添加失败';
            ajax_error($msg);
        }
        break;
    case "get_order_detail":
        //获取订单号
        $id = $_GPC['id'];
        $member = unserialize(base64_decode($_SESSION['vj_member']));
        $type = $_GPC['type'];
        if (empty($id)) ajax_error('参数错误');
        switch ($type) {
            case "single":
                $order = pdo_fetch("SELECT * FROM "
                    . tablename('bontrip_single_order') . " WHERE uniacid = '{$uniacid}' AND member_id = {$member['id']} AND id = {$id} AND deleted_at IS NULL");
                break;
            case "oneday":
                $order = pdo_fetch("SELECT * FROM "
                    . tablename('bontrip_oneday_order') . " WHERE uniacid = '{$uniacid}' AND member_id = {$member['id']} AND id = {$id} AND deleted_at IS NULL");
                break;
            case "quality":
                $order = pdo_fetch("SELECT * FROM "
                    . tablename('bontrip_quality_order') . " WHERE uniacid = '{$uniacid}' AND member_id = {$member['id']} AND id = {$id} AND deleted_at IS NULL");
                break;
            case "insurance":
                $order = pdo_fetch("SELECT * FROM "
                    . tablename('bontrip_insurance_order') . " WHERE uniacid = '{$uniacid}' AND member_id = {$member['id']} AND id = {$id} AND deleted_at IS NULL");
                break;
            default:
                ajax_error('参数错误');
                break;
        }
        if (empty($order)) ajax_error('订单不存在');
        if ($order['status'] == 1) ajax_error('订单已支付');
        ajax_success($order);
        break;
    case "del_order":
        $id = $_GPC['id'];
        $member = unserialize(base64_decode($_SESSION['vj_member']));
        $type = $_GPC['type'];
        if (empty($id)) ajax_error('参数错误');
        switch ($type) {
            case "single":
                $order = pdo_fetch("SELECT * FROM "
                    . tablename('bontrip_single_order') . " WHERE uniacid = '{$uniacid}' AND member_id = {$member['id']} AND id = {$id} AND deleted_at IS NULL");
                break;
            case "oneday":
                $order = pdo_fetch("SELECT * FROM "
                    . tablename('bontrip_oneday_order') . " WHERE uniacid = '{$uniacid}' AND member_id = {$member['id']} AND id = {$id} AND deleted_at IS NULL");
                break;
            case "quality":
                $order = pdo_fetch("SELECT * FROM "
                    . tablename('bontrip_quality_order') . " WHERE uniacid = '{$uniacid}' AND member_id = {$member['id']} AND id = {$id} AND deleted_at IS NULL");
                break;
            case "insurance":
                $order = pdo_fetch("SELECT * FROM "
                    . tablename('bontrip_insurance_order') . " WHERE uniacid = '{$uniacid}' AND member_id = {$member['id']} AND id = {$id} AND deleted_at IS NULL");
                break;
            default:
                ajax_error('参数错误');
                break;
        }
        if (empty($order)) ajax_error('订单不存在');
        if ($order['status'] == 1) ajax_error('订单已支付,不能删除!');

        pdo_update('bontrip_' . $type . '_order', array('deleted_at' => TIMESTAMP), array('id' => $order['id']));
        ajax_success(array());
        break;
    case "mobile_verification":
        //修改手机验证
        $member = unserialize(base64_decode($_SESSION['vj_member']));
        $authcode = $_GPC['authcode'];
        /*
         * 手机验证码判断
         */
        $sms_last = pdo_fetch("SELECT * FROM " . tablename('bontrip_auth_code')
            . " WHERE mobile = '{$member['mobile']}' ORDER BY created_at DESC");
        if (empty($sms_last)) {
            ajax_error('验证失败！');
        }

        if ($sms_last['auth_code'] != $authcode) {
            ajax_error('验证码错误！');
        }
        if (TIMESTAMP - $sms_last['created_at'] > 300) {
            ajax_error('验证码已被使用或超时，请重新获取验证码！');
        }
        ajax_success(array());
        break;
    case "mobile_edit":
        //修改手机
        $member = unserialize(base64_decode($_SESSION['vj_member']));
        $mobile = $_GPC['mobile'];
        $authcode = $_GPC['authcode'];
        /*
         * 手机验证码判断
         */
        $sms_last = pdo_fetch("SELECT * FROM " . tablename('bontrip_auth_code')
            . " WHERE mobile = '{$mobile}' ORDER BY created_at DESC");
        if (empty($sms_last)) {
            ajax_error('验证失败！');
        }

        if ($sms_last['auth_code'] != $authcode) {
            ajax_error('验证码错误！');
        }
        if (TIMESTAMP - $sms_last['created_at'] > 300) {
            ajax_error('验证码已被使用或超时，请重新获取验证码！');
        }
        $member_info = pdo_fetch("SELECT * FROM " . tablename('bontrip_member')
            . " WHERE mobile = {$mobile} AND uniacid={$uniacid} AND id != {$member['id']} AND deleted_at is null");
        if (!empty($member_info)) {
            ajax_error('手机已存在！');
        }

        $data = array(
            'mobile' => $mobile
        );
        pdo_update('bontrip_member', $data, array('id' => $member['id']));
        $member['mobile'] = $mobile;
        $_SESSION['vj_member'] = base64_encode(iserializer($member));
        ajax_success(array(), '手机重置成功');
        break;
    case "email_edit":
        //修改邮箱
        //验证格式
        $member = unserialize(base64_decode($_SESSION['vj_member']));
        $email = $_GPC['email'];
        $data = array(
            'email' => $email
        );
        if (!preg_match('/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/', $email)) ajax_error('邮箱格式错误！');

        pdo_update('bontrip_member', $data, array('id' => $member['id']));
        $member['email'] = $email;
        $_SESSION['vj_member'] = base64_encode(iserializer($member));
        ajax_success(array(), '邮箱重置成功');
        break;
    case "real_name_edit":
        //修改真实姓名
        //验证格式
        $member = unserialize(base64_decode($_SESSION['vj_member']));
        $real_name = $_GPC['real_name'];
        $data = array(
            'real_name' => $real_name
        );
        if (empty($real_name)) ajax_error('请填写真实姓名');

        pdo_update('bontrip_member', $data, array('id' => $member['id']));
        $member['real_name'] = $real_name;
        $_SESSION['vj_member'] = base64_encode(iserializer($member));
        ajax_success(array(), '真实姓名成功');
        break;
    default:
        $msg = '参数错误，请求失败';
        ajax_error($msg);
        break;
}

function ajax_error($message)
{
    exit(json_encode(array('status' => 0, 'msg' => $message)));
}

function ajax_success($data, $message = '请求成功')
{
    exit(json_encode(array('status' => 1, 'msg' => $message, 'data' => $data)));

}

/**
 * 订单单号
 * 生成支付单编号(标识码+年月日+会员ID%1000+微秒+3位随机)
 * 长度:1+8+3+3+3
 *
 * @param $member_id
 * @return string
 */
function orderNo($member_id)
{
    return '1' . date('Ymd')
        . sprintf('%03d', (int)$member_id % 1000)
        . sprintf('%03d', (float)microtime() * 1000)
        . mt_rand(100, 999);
}